﻿#include "OiPCH.hpp"
#include "View/OiPixmap.hpp"

namespace Oi
{
    namespace PLScannerCore
    {
        class PresetWidgetImpl : public Impl<PresetWidget>
        {
        public:
            void init();
            void updateWidget();
            void updateParams();

        public:
            void clearView();

        public:
            PresetParams _params;

        public:
            QVariantMap _config;

        public:
            bool _editable;
            QString _namecpy;

        public:
            QWidget* _title;

            QLabel* _nameLabel;
            QLineEdit * _nameEdit;

            QToolButton* _rro;
            QToolButton* _lro;
            QToolButton* _splice;
//             QToolButton* _edge;
//             QToolButton* _width;
            QToolButton* _circum;
            QToolButton* _con;

            ToolProfilesView* _toolsViewer;

        };

        void PresetWidgetImpl::init()
        {
            OI_Q(PresetWidget);

            q->setFocusPolicy(Qt::ClickFocus);
            _editable = false;

            QVBoxLayout* mainLayout = new QVBoxLayout(q);
            mainLayout->setContentsMargins(0, 0, 0, 0);

            _title = new QWidget(q);
            //tWidget->setStyleSheet(QString("background:%1").arg(PLScanner::instance()->zColorBg.name()));
            auto tLayout = new QHBoxLayout(_title);
            tLayout->addStretch(1);

            _nameLabel = new QLabel(PresetWidget::tr("产品名称:"), q);
            _nameEdit = new QLineEdit(q);

            _nameLabel->setStyleSheet(QString("color:%1;font-size:12pt").arg(PLScannerStyle::windowTextColor().rgb(), 6, 16, QChar('0')));

            _nameEdit->setReadOnly(true);
            _nameEdit->setStyleSheet(QString("background:transparent;border-width:0;border-style:outset;color:%1;font-size:12pt")
                .arg(PLScannerStyle::windowTextColor().rgb(), 6, 16, QChar('0')));
            _nameEdit->installEventFilter(q);

            tLayout->addWidget(_nameLabel);
            tLayout->addWidget(_nameEdit);
            tLayout->addStretch(5);

            _rro = new QToolButton(q);
            _rro->setIcon(QIcon(":/resources/"+PLScanner::instance()->resourceName+"/rro_light.png"));
            _rro->setText(PresetWidget::tr("径向跳动"));
            _rro->setStyleSheet(QString("QToolButton{color:%1}QToolButton:hover,QToolButton:checked{color:black}").arg(PLScannerStyle::windowTextColor().rgb(), 6, 16, QChar('0')));
            _rro->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);

            _lro = new QToolButton(q);
            _lro->setIcon(QIcon(":/resources/"+PLScanner::instance()->resourceName+"/lro_light.png"));
            _lro->setText(PresetWidget::tr("侧向偏摆"));
            _lro->setStyleSheet(QString("QToolButton{color:%1}QToolButton:hover,QToolButton:checked{color:black}").arg(PLScannerStyle::windowTextColor().rgb(), 6, 16, QChar('0')));
            _lro->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);

            _splice = new QToolButton(q);
            _splice->setIcon(QIcon(":/resources/"+PLScanner::instance()->resourceName+"/splice_light.png"));
            _splice->setText(PresetWidget::tr("接头检测"));
            _splice->setStyleSheet(QString("QToolButton{color:%1}QToolButton:hover,QToolButton:checked{color:black}").arg(PLScannerStyle::windowTextColor().rgb(), 6, 16, QChar('0')));
            _splice->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);

//             _edge = new QToolButton(q);
//             _edge->setIcon(QIcon(":/resources/"+PLScanner::instance()->resourceName+"/edge_light.png"));
//             _edge->setText(PresetWidget::tr("边缘检测"));
//             _edge->setStyleSheet("QToolButton{color:white}QToolButton:hover,QToolButton:checked{color:black}");
//             _edge->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
// 
//             _width = new QToolButton(q);
//             _width->setIcon(QIcon(":/resources/"+PLScanner::instance()->resourceName+"/width_light.png"));
//             _width->setText(PresetWidget::tr("宽度检测"));
//             _width->setStyleSheet("QToolButton{color:white}QToolButton:hover,QToolButton:checked{color:black}");
//             _width->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);

            _circum = new QToolButton(q);
            _circum->setIcon(QIcon(":/resources/" + PLScanner::instance()->resourceName + "/edge_light.png"));
            _circum->setText(ProductWidget::tr("周长检测"));
            _circum->setStyleSheet(QString("QToolButton{color:%1}QToolButton:hover,QToolButton:checked{color:black}").arg(PLScannerStyle::windowTextColor().rgb(), 6, 16, QChar('0')));
            _circum->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);

            _con = new QToolButton(q);
            _con->setIcon(QIcon(":/resources/"+PLScanner::instance()->resourceName+"/con_light.png"));
            _con->setText(PresetWidget::tr("锥度检测"));
            _con->setStyleSheet(QString("QToolButton{color:%1}QToolButton:hover,QToolButton:checked{color:black}").arg(PLScannerStyle::windowTextColor().rgb(), 6, 16, QChar('0')));
            _con->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);

            tLayout->addWidget(_rro);
            tLayout->addWidget(_lro);
            tLayout->addWidget(_splice);
            tLayout->addWidget(_circum);
            tLayout->addWidget(_con);

            _title->setEnabled(false);

            _toolsViewer = new ToolProfilesView(ToolProfilesView::VM_PresetView, q);

            mainLayout->addWidget(_title);
            mainLayout->addWidget(_toolsViewer);

            q->connect(_nameEdit, &QLineEdit::editingFinished, q, &PresetWidget::onEditFinished);

            q->connect(_rro, &QToolButton::clicked, q, &PresetWidget::onAddTool);

            q->connect(_lro, &QToolButton::clicked, q, &PresetWidget::onAddTool);

            q->connect(_splice, &QToolButton::clicked, q, &PresetWidget::onAddTool);

            q->connect(_circum, &QToolButton::clicked, q, &PresetWidget::onAddTool);

            q->connect(_con, &QToolButton::clicked, q, &PresetWidget::onAddTool);

        }

        void PresetWidgetImpl::updateWidget()
        {
            _toolsViewer->setPresetParams(_params);

            _toolsViewer->zoomFit();
        }
       
        void PresetWidgetImpl::updateParams()
        {
            _params.clear();

            _params = _toolsViewer->presetParams();
        }

        void PresetWidgetImpl::clearView()
        {
            _toolsViewer->onClearView();
        }

        PresetWidget::PresetWidget(QWidget * parent /*= nullptr*/) : QWidget(parent)
        {
            OI_I(PresetWidget)->init();
        }

        void PresetWidget::setParams(const QString name, const PresetParams params)
        {
            OI_F(PresetWidget);

            d->_nameEdit->setText(name);
            d->_params = params;
            d->updateWidget();
        }

        PresetParams PresetWidget::parmas()
        {
            OI_F(PresetWidget);

            d->updateParams();

            return d->_params;
        }

        QString PresetWidget::name() const
        {
            return OI_IMPL()->_nameEdit->text();
        }

        void PresetWidget::setEditable(bool enable)
        {
            OI_F(PresetWidget);
            d->_editable = enable;
            d->_toolsViewer->setEditable(enable);

            d->_title->setEnabled(enable);
    
            if (enable)
            {
                d->_namecpy = d->_nameEdit->text();
            }
        }

        bool PresetWidget::eventFilter(QObject *obj, QEvent *evt)
        {
            OI_F(PresetWidget);

            if(evt->type() == QEvent::MouseButtonDblClick)
            {
                auto edit = qobject_cast<QLineEdit*>( obj );
                if (edit && d->_editable)
                {
                    edit->setReadOnly(false);
                    edit->selectAll();
                    edit->setStyleSheet("background:white;border-width:0;border-style:outset;color:black;font-size:12pt");
                    return true;
                }
            }

            return QObject::eventFilter(obj, evt);
        }

        void PresetWidget::onEditFinished()
        {
            OI_F(PresetWidget);

            auto edit = qobject_cast<QLineEdit*>( sender() );

            edit->setReadOnly(true);
            edit->setStyleSheet(QString("background:transparent;border-width:0;border-style:outset;color:%1;font-size:12pt")
                .arg(PLScannerStyle::windowTextColor().rgb(), 6, 16, QChar('0')));

            if(d->_editable)
            {
                if(edit == d->_nameEdit)
                {
                    auto name = edit->text();

                    if (name != d->_namecpy && ProductParams::checkName(name))
                    {
                        for (int i = 0; i < 99; i++)
                        {
                            auto temp = name + "_" + QString::number(i);
                            if (!ProductParams::checkName(temp))
                            {
                                edit->setText(temp);
                                break;
                            }
                        }
                        OiAlerter() << tr("已存在当前配方");
                    }
                }
            }
        }

        void PresetWidget::onAddTool()
        {
            OI_F(PresetWidget);

            auto obj = sender();

            if (obj == d->_rro)
            {
                d->_toolsViewer->onAddTool(BalanceTool::RadialBalance);
            }
            else if (obj == d->_lro)
            {
                d->_toolsViewer->onAddTool(BalanceTool::LateralBalance);
            }
            else if(obj == d->_splice)
            {
                d->_toolsViewer->onAddTool(BalanceTool::SpliceChecker);
            }
            else if(obj == d->_circum)
            {
                d->_toolsViewer->onAddTool(BalanceTool::Perimeter);
            }
            else if(obj == d->_con)
            {
                d->_toolsViewer->onAddTool(BalanceTool::ConChecker);
            }
        }


        void PresetWidget::onClearView()
        {
            OI_F(PresetWidget);
            d->clearView();
        }
    }
}